//
//  WXMagicLabel.m
//  数字变化动效
//
//  Created by tianlong on 2018/6/5.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "WXMagicLabel.h"
#import "Masonry.h"
#import "UIColor+WXUtility.h"
#import "UIFont+WXFont.h"

@interface WXMagicLabel ()
@end

@implementation WXMagicLabel

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (void)setNumberTextWtihRollAniamtionFromNum:(CGFloat)fromNum
                                        toNum:(CGFloat)toNum
                                     duration:(CFTimeInterval)duration
                                animationType:(MagicAnimationType)type
                                progressBlock:(void (^)(CGFloat number,CGFloat percent))progressBlock {
    [[WXMagicNumber number] animation_rollFromNumber:fromNum toNumber:toNum animationType:type duration:duration progressBlock:^(CGFloat number, CGFloat percent) {
        if (progressBlock) {
            progressBlock(number,percent);
        }
    }];
}

- (void)showTipLabelWithNumber:(CGFloat)num
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                   otherConfig:(void (^)(UILabel *tipLabel))otherConfig {
    __weak typeof(self)ws = self;
    [self removeTipLabel];
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = [NSString stringWithFormat:@"+%.0f",num];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = font?font:[UIFont fontPingFangSCWithSize:8];
    tipLabel.textColor = textColor?textColor:[UIColor whiteColor];
    self.tipLabel = tipLabel;
    [self addSubview:tipLabel];
    self.tipLabel.alpha = 1;
    
    UILabel *temp = [[UILabel alloc] init];
    temp.font = tipLabel.font;
    temp.text = tipLabel.text;
    [temp sizeToFit];
    CGFloat w = temp.width+8;
    CGFloat h = temp.height+2;
    [self.tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.centerY.equalTo(ws.mas_centerY);
        make.height.offset(h);
        make.width.offset(w);
    }];
    if (otherConfig) {
        otherConfig(tipLabel);
    }
    /*橘黄色背景
     tipLabel.backgroundColor = [UIColor colorWithHexValue:0xF5A623];
     tipLabel.layer.cornerRadius = tipLabel.height*0.5;
     tipLabel.layer.masksToBounds = YES;
     */
}

- (void)removeTipLabel {
    if (self.tipLabel) {
        [self.tipLabel removeFromSuperview];
        self.tipLabel = nil;
    }
}

@end

